import axios from 'axios'
import qs from 'qs'
import { Message } from 'element-ui';
// 创建axios实例
const service = axios.create({
	baseURL:'https://testelp.quality-in.com',
  	timeout: 15000, // 请求超时时间
  
})

// request拦截器
service.interceptors.request.use(config => {
	let datas = qs.parse(config.data);
	if(!datas["jsonType"]){
		if(datas['vue_qs_type']){
			  delete config.data["vue_qs_type"];
				config.data = qs.stringify(config.data,{ allowDots: true });	
		}else{
			config.data = qs.stringify(config.data,{ indices: false });
		}	
	}else{
		config.data = JSON.stringify(config.data);
	}
	
  return config
}, error => {
  // Do something with request error
	  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    const data = response.data;
    if(data.status!= 1){
    	Message({
	      message: data.message,
	      type: 'error',
	      duration: 2000
	    });
	    return;
    }else{
    	return response.data;	
    }
    
  },
  error => {

    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
